package com.eb.controller;
import com.eb.context.BaseContext;
import com.eb.dto.EditUserDto;
import com.eb.dto.RegisterUserDto;
import com.eb.dto.UserLoginDto;
import com.eb.exception.BaseException;
import com.eb.result.Result;
import com.eb.service.UserService;
import com.eb.vo.GetInfoUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户操作")
@CrossOrigin//解决跨域问题
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param userDto
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody RegisterUserDto userDto){
        boolean b=userService.register(userDto);
        if (b)
        {
            return Result.success();
        }else
            throw new BaseException("用户信息不合法");
    }

    /**
     * 用户登录
     * @param
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody UserLoginDto userLoginDto){
        String token = userService.login(userLoginDto.getUsername(), userLoginDto.getPassword());
        return Result.success(token);
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @GetMapping("/info")
    @ApiOperation("根据id获取用户信息")
    public Result<GetInfoUserVO> getUserInfo(){
        log.info("获取id为{}的用户信息", BaseContext.getCurrentId());
        GetInfoUserVO getInfoUserVO = userService.getUserInfo(BaseContext.getCurrentId());
        return Result.success(getInfoUserVO);
    }

    /**
     * 编辑用户信息
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation("修改用户信息")
    public Result<String> updateUserInfo(@RequestBody EditUserDto userDto){
        log.info("修改用户信息,员工信息为{}",userDto);
        userService.updateUserInfo(userDto);
        return Result.success();
    }
}
